package main

import (
	"fmt"
	"math/rand"
	"sort"
	"strconv"
)

type Stu struct {
	Name string
	Age int
	Score float64
}

type StuSlice []Stu

// 实现sort.Sort的三个方法
func (stu StuSlice) Len() int {
	return len(stu)
}
func (stu StuSlice) Less(i, j int) bool {
	return stu[i].Score < stu[j].Score  // 升序排列
	//return stu[i].Score > stu[j].Score  // 降序排列
}
func (stu StuSlice) Swap(i, j int) {
	stu[i], stu[j] = stu[j], stu[i]
}


func main() {
	var stus StuSlice
	for i := 0;i<10;i++{
		//rand.Float64()*50 + 40  50为标准差，40为期望
		score, _ :=strconv.ParseFloat((fmt.Sprintf("%.2f", rand.Float64()*50 + 40)), 64)
		stu := Stu{
			Name: fmt.Sprintf("stu-%d", rand.Intn(100)),
			Age: rand.Intn(30),
			Score: score,
		}
		stus = append(stus, stu)
	}
	sort.Sort(stus)
	fmt.Println(stus)
}
